<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Models\System\File;
use Carbon\Carbon;
use Illuminate\Http\Request;

class FileService
{
    use Service;

    /**
     * 上传文件
     *
     * @param Request $request
     */
    public function FileIndexService( $request )
    {
        $where    = function ( $query ) use ( $request ) {
            if ( $request->filled('keyword') and $request->input('keyword') != '' ) {
                $value = "%" . $request->input('keyword') . "%";
                $query->where('name', 'like', $value);
            }
            if ( $request->filled('ext') and $request->input('ext') != '' ) $query->where('ext', $request->input('ext'));
            if ( $request->filled('uid') and $request->input('uid') != '' ) $query->where('uid', $request->input('uid'));
            if ( $request->filled('category') and $request->input('category') != '' ) $query->where('category', $request->input('category'));
            if ( $request->filled('user') and $request->input('user') != '' ) $query->where('user', $request->input('user'));
            $query->where('status', 1);
        };
        $with     = [
            'user'    => function ( $query ) {
                $query->select('id', 'name');
            },
            'primary' => function ( $query ) {
                $query->select('url', 'file_id', 'file_url');
            },
        ];
        $select   = [
            'id', 'size', 'name', 'hash', 'url', 'file_url', 'created_at', 'ext', 'uid', 'category', 'user',
        ];
        $ext      = File::SelectGetOlyModel('ext', 'ext');
        $uid      = File::SelectGetOlyModel('uid', 'uid');
        $category = File::SelectGetOlyModel('category', 'category');
        $user     = File::SelectGetOlyModel('user', 'user');
        $file     = File::PaginateModel($where, $with, $select);
        $file->appends([
            'keyword'  => $request->input('keyword'),
            'ext'      => $request->input('ext'),
            'uid'      => $request->input('uid'),
            'category' => $request->input('category'),
            'user'     => $request->input('user'),
            'static'   => $request->input('static'),
        ])->render();
        return view('system.file.index', compact('file', 'ext', 'uid', 'category', 'user', 'request'));
    }

    /**
     * 删除上传的文件
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function FileDestroyService( $request )
    {
        $error                = $this->getErrorCode();
        $where                = function ( $query ) use ( $request ) {
            $query->where('id', $request->input('id'));
        };
        $data[ 'status' ]     = $request->input('status', 2);
        $data[ 'deleted_at' ] = Carbon::now()->toDateTimeString();
        File::UpdateModel($where, $data);
        return back()->with('success', $error::message(204));
    }

    /**
     * 批量删除上传文件
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function FileDeleteService( $request )
    {
        $where                = function ( $query ) use ( $request ) {
            if ( is_array($request->input('id')) ) $query->whereIn('id', $request->input('id'));
            else $query->where('id', $request->input('id'));
        };
        $data[ 'status' ]     = $request->input('status', 2);
        $data[ 'deleted_at' ] = Carbon::now()->toDateTimeString();
        File::UpdateModel($where, $data);
        return data();

    }

    /**
     * 还原上传的文件
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function FileReductionService( $request )
    {
        $error = $this->getErrorCode();
        switch ( $request->input('static') ) {
            case 1:
                $where                = function ( $query ) use ( $request ) {
                    $query->where('id', $request->input('id'));
                };
                $data[ 'status' ]     = 1;
                $data[ 'deleted_at' ] = NULL;
                File::UpdateModel($where, $data);
                return back()->with('success', $error::message(208));
                break;
            default:
                return back()->with('error', $error::message(1001));
                break;
        }
    }

    /**
     * 批量还原上传文件
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function FileAllReductionService( $request )
    {
        switch ( $request->input('static') ) {
            case 1:
                $where                = function ( $query ) use ( $request ) {
                    $query->whereIn('id', $request->input('id'));
                };
                $data[ 'status' ]     = 1;
                $data[ 'deleted_at' ] = NULL;
                File::UpdateModel($where, $data);
                return data();
                break;
            default:
                return data(NULL, $this->getErrorCode()::message(1001), 1000);
                break;
        }
    }

}
